package main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;

import com.hichip.base.HiLog;
import com.hichip.campro.R;
import com.hichip.sdk.HiChipSDK;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;
import common.HiDataValue;
import utils.SharePreUtils;
/**
 * launcher界面
 * Created by lt on 2017\12\14 0014.
 */
public class SplashActivity extends AppCompatActivity {
    private final static int HANDLE_MESSAGE_INIT_END = 0x90000001;
    private long initSdkTime;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //初始化HiSDK.jar
        initSDK();
        initXGPushSDK();
    }

    // 初始化SDK
    private void initSDK() {
        initSdkTime = System.currentTimeMillis();
        HiChipSDK.init(new HiChipSDK.HiChipInitCallback() {
            @Override
            public void onSuccess(int syhandle, int xqhandle) {
                if (syhandle >= 0 && xqhandle >= 0) {
                    Message msg = mHandler.obtainMessage();
                    msg.what = HANDLE_MESSAGE_INIT_END;
                    mHandler.sendMessage(msg);
                    HiLog.e("SDK INIT success");
                }
            }

            @Override
            public void onFali(int syhandle, int xqhandle) {
                Message msg = mHandler.obtainMessage();
                msg.what = HANDLE_MESSAGE_INIT_END;
                mHandler.sendMessage(msg);
                HiLog.e("SDK INIT fail");
            }
        });
    }

    private void initXGPushSDK() {
        // 开启logcat输出，方便debug，发布时请关闭
        XGPushConfig.enableDebug(this, false);
        XGPushManager.registerPush(this, new XGIOperateCallback() {
            @Override
            public void onSuccess(Object data, int flag) {
                String token = (String) data;
                Log.i("tedu", "-信鸽-Token--:"+token);
                String localToken= SharePreUtils.getString("XGToken", SplashActivity.this, "xgtoken");
                if(!TextUtils.isEmpty(token)&&TextUtils.isEmpty(localToken)&&!token.equalsIgnoreCase(localToken)){
                    SharePreUtils.putString("XGToken",SplashActivity.this, "xgtoken", token);
                }
                if(!TextUtils.isEmpty(token)){
                    HiDataValue.XGToken = token;
                }
            }
            @Override
            public void onFail(Object data, int errCode, String msg) {
                Log.i("tedu", "-bruce 注册失败，为--:"+msg);
            }
        });
        HiDataValue.XGToken= SharePreUtils.getString("XGToken", this, "xgtoken");
    }

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            long spendingTime = System.currentTimeMillis() - initSdkTime;
            if (spendingTime < 2000 && spendingTime > 0) {

                this.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startMainActivity();
                    }
                }, 2000 - spendingTime);

            } else {
                startMainActivity();
            }
        }
    };
    private void startMainActivity() {
        Intent intent=new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
        finish();
    }


}
